package com.cloudkinto.controller.finance;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.finance.vo.RechargeRecordMatchInvoiceReq;
import com.cloudkinto.service.finance.RechargeRecordService;
import com.cloudkinto.service.finance.vo.RechargeAddReq;
import com.cloudkinto.service.finance.vo.RechargeRecordCheckReq;
import com.cloudkinto.service.finance.vo.RechargeRecordRes;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ding
 * @since 2020-06-10
 */
@RestController
@Api(tags = "财务-充值模块")
@RequestMapping("/api/admin/recharge")
public class RechargeRecordController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(RechargeRecordController.class);

    @Autowired
    private RechargeRecordService service;

    @UserSessionRequired
    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit(getCompanyId());
    }

    @UserSessionRequired
    @PostMapping("/add")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@Validated @RequestBody RechargeAddReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("申请成功，请等待审核", service.add(req, companyId, userId));
    }

    @UserSessionRequired
    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @UserSessionRequired
    @PutMapping("")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated Object req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(id, userId));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间"),
            @ApiImplicitParam(name = "endDate", value = "结束时间"),
            @ApiImplicitParam(name = "type", value = "pageInit 返回数据")})
    @ApiResponses({@ApiResponse(code = 200, message = "", response = RechargeRecordRes.class)})
    public SingleResult list(String startDate, String endDate, String type) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("type", type);
        if (companyId != null) {
            map.put("companyId", companyId + "");
        }
        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "startDate", value = "开始时间"),
            @ApiImplicitParam(name = "endDate", value = "结束时间"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
            @ApiImplicitParam(name = "rechargeTypeId", value = "充值方式"),
            @ApiImplicitParam(name = "doubleCheck", value = "二次确认  空全部 0 未确认"),
            @ApiImplicitParam(name = "type", value = "订单类型 空是全部 0 待审核  1充值成功 2充值取消")})
    @ApiResponses({@ApiResponse(code = 200, message = "Success", response = RechargeRecordRes.class)})//无效
    public PageResult<RechargeRecordRes> listPage(
            String startDate,
            String endDate,
            String type,
            String rechargeTypeId,
            String doubleCheck,
            String current, String pageSize, String companyId) {
        Map<String, String> map = new HashMap<>();
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("type", type);
        map.put("rechargeTypeId", rechargeTypeId);
        map.put("current", current);
        map.put("pageSize", pageSize);
        if (StringUtils.isNotBlank(doubleCheck)) {
            map.put("doubleCheck", doubleCheck);
        }
        if (StringUtils.isNotBlank(companyId)) {
            map.put("companyId", companyId);
        }
        return service.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("/matchInvoice/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "startDate", value = "开始时间"),
            @ApiImplicitParam(name = "companyId", value = "公司id"),
            @ApiImplicitParam(name = "endDate", value = "结束时间"),
            @ApiImplicitParam(name = "currency", value = "币种"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
            @ApiImplicitParam(name = "matchType", value = "匹配类型 空是全部 0 未匹配  1已匹配")})
    @ApiResponses({@ApiResponse(code = 200, message = "Success", response = RechargeRecordRes.class)})//无效
    public PageResult<RechargeRecordRes> matchInvoice(
            String startDate,
            String endDate,
            String matchType,
            String currency,
            String companyId,
            String current, String pageSize) {
        Map<String, String> map = new HashMap<>();
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("matchType", matchType);
        map.put("currency", currency);
        map.put("current", current);
        map.put("pageSize", pageSize);
        if (StringUtils.isNotBlank(companyId)) {
            map.put("companyId", companyId);
        }
        return service.matchInvoiceList(map);
    }

    @UserSessionRequired
    @PostMapping("/checkPass")
    @ApiOperation(value = "审核入账", notes = "审核入账")
    public SingleResult checkPass(@RequestBody RechargeRecordCheckReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.checkPass(req, userId));
    }

}

